﻿package 
{
	import flash.utils.Timer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;

	public class BattleField extends MovieClip
	{
		private var gameTimer:Timer = new Timer(1000,0);// interval, loop
		private var timeVar:int = 0;
		private var bombTimer:int;
		private var squareSize:int = 50;
		private var columnsX:int = 19;
		private var columnsY:int = 10;
		private var level:Array = new Array();
		private var blocks:Array = new Array();
		private var bombs:Array = new Array();
		private var tileType:int;
		private var border:int = 25;
		
		public function BattleField()
		{
			// constructor code
			battleFieldArray();
			createGrid();
			gameTimer.addEventListener(TimerEvent.TIMER, TimeUpdate);
			gameTimer.start();
		}
		
		public function checkCollisions(player:Player) : Boolean {
			for (var index in blocks) {
				var block:Block = blocks[index];
				var type = block.getTileType();
				
				if (type == 1 || type == 2) {
					var hit:Boolean = player.clip.hitTestObject(block.tile);
				
					if (hit) {
						return true;
					}
				}
			}
			
			return false;
		}

		private function TimeUpdate(event:Event)
		{
			timeVar++;
			for (var i:int=0; i<bombs.length; i++)
			{
				bombTimer = bombs[i].getCreateTime();
				if (timeVar == bombTimer+3)
				{
					trace("BBBBBBBBBBBOOOOOOOOOOOOOOOOOOOOOOOOOOMMMM!!!!!!!!!!!!");
					bombs[i].clip.scaleY = 2;
					bombs[i].clip.scaleX = 2;
					
					bombs[i].clip.x = bombs[i].clip.x - 25
					bombs[i].clip.y = bombs[i].clip.y - 25
					bombs[i].clip.alpha = 0.4;
				}
				if (timeVar == bombTimer+4)
				{
					removeChild(bombs[i].clip);
				}

			}
		}


		private function battleFieldArray()
		{
			//                   X "1","2","3","4","5","6","7","8","9","0","1","2","3","4","5","6","7","8","9","0"
			level[0] = new Array("0","2","0","2","0","2","0","2","0","2","0","2","0","2","0","2","0","2","0","2");
			level[1] = new Array("0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1");
			level[2] = new Array("0","0","0","0","0","0","0","0","2","0","0","2","0","2","0","2","0","2","0","2");
			level[3] = new Array("0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1");
			level[4] = new Array("0","0","0","2","2","2","0","0","2","0","0","2","0","2","0","2","0","2","0","2");
			level[5] = new Array("0","1","2","1","2","1","2","1","0","1","0","1","0","1","0","1","0","1","0","1");
			level[6] = new Array("2","2","2","2","2","2","2","0","2","2","0","2","0","2","0","2","0","2","0","2");
			level[7] = new Array("0","1","2","1","2","1","2","1","0","1","0","1","0","1","0","1","0","1","0","1");
			level[8] = new Array("0","0","2","0","2","2","2","0","0","0","0","2","0","2","0","2","0","2","0","2");
			level[9] = new Array("0","1","2","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1","0","1");
			level[10] = new Array("0","0","2","0","0","0","0","0","0","0","0","2","0","2","0","2","0","2","0","2");
			// mapx = <-- -->, x is horizontaal
			// mapy =  up down, y is vertikaal
		}

		public function getLevel():Array
		{
			return level;
		}

		public function getBlocks():Array
		{
			return blocks;
		}

		//Maak map
		private function createGrid()
		{
			for (var mapy:int=0; mapy<columnsY; mapy++)
			{
				for (var mapx:int=0; mapx<columnsX; mapx++)
				{
					//trace(level[mapy][mapx]);

					var x:int = mapx * squareSize + border;
					var y:int = mapy * squareSize + border;

					tileType = level[mapy][mapx];

					var block:Block = new Block(x,y,tileType);

					blocks.push(block);
					this.addChild(block.tile);
				}
			}
		}//sluit function

		private function calcColumnX(posX:int):int
		{
			if (posX < 0)
			{
				return -1;
			}

			if (posX > (0 + (squareSize * columnsX)))
			{
				return -1;
			}

			var column:int = (posX - 0) / squareSize;

			if (column >= columnsX)
			{
				return -1;
			}

			return column;
		}

		private function calcColumnY(posY:int):int
		{
			if (posY < 0)
			{
				return -1;
			}

			if (posY > (0 + (squareSize * columnsY)))
			{
				return -1;
			}

			var column:int = (posY - 0) / squareSize;

			if (column >= columnsY)
			{
				return -1;
			}

			return column;
		}

		// Bommen plaatsen
		public function placeBomb(player:Player)
		{//player
			var columnX:int = calcColumnX(player.clip.x);
			var columnY:int = calcColumnX(player.clip.y);

			var newPosX:int = columnX * squareSize + border;
			var newPosY:int = columnY * squareSize + border;

			var bomb:Bomb = new Bomb(timeVar,player,newPosX,newPosY);
			bombs.push(bomb);
			this.addChild(bomb.clip);
		}

	}// sluit class

}// sluit package